package com.hzhk.happyrent.ui.activity.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.App;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.view.PredicateLayout;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditOrderNoActivity extends Activity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.act_login_submit)
    Button actLoginSubmit;
    @BindView(R.id.pre_otder_phone_image)
    ImageView preOtderPhoneImage;
    @BindView(R.id.pre_otder_phone_name)
    TextView preOtderPhoneName;
    @BindView(R.id.layout_product_type)
    PredicateLayout predicateLayout;
    @BindView(R.id.pre_price)
    TextView prePrice;
    @BindView(R.id.edit_order_address)
    TextView editOrderAddress;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_order_no);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        intent = getIntent();
        //addressPrePhone.setText(intent.getStringExtra(Constants.MOBILE));
        //addressPreName.setText(intent.getStringExtra(Constants.CONSIGNEE));
        prePrice.setText(intent.getStringExtra(Constants.TOTAL_PRICE)+"元");
        preOtderPhoneName.setText(intent.getStringExtra(Constants.GOODS_NAME));
        ShowImageUtils.showImageViewToCircleNoPlaceHolder(App.getContext(), intent.getStringExtra(Constants.GOODS_IMAGE), preOtderPhoneImage, 0);
        String attr_key_path = intent.getStringExtra(Constants.ATTR_KEY_PATH);
        if (!TextUtils.isEmpty(attr_key_path)) {
            predicateLayout.removeAllViews();
            String description = attr_key_path.substring(1, attr_key_path.length() - 1);
            if (description.contains("][")) {
                String[] tag = description.split("]\\u005B");
                for (String s : tag) {
                    TextView textView = new TextView(this);
                    textView.setText(s);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    textView.setPadding(5, 0, 5, 0);
                    textView.setTextColor(this.getResources().getColor(R.color.tab_red_80));
                    //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                    textView.setBackground(this.getResources().getDrawable(R.drawable.bg_red_hollow));
                    predicateLayout.addView(textView);
                }
            } else {
                TextView textView = new TextView(this);
                textView.setText(description);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                textView.setPadding(5, 0, 5, 0);
                textView.setTextColor(this.getResources().getColor(R.color.tab_red_80));
                //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                textView.setBackground(this.getResources().getDrawable(R.drawable.bg_red_hollow));
                predicateLayout.addView(textView);
            }
        }
        //preOtderPhoneName.setText(intent.getStringExtra(Constants.CONSIGNEE));
    }

    private void initView() {
        activityTitlebarCenter.setText(R.string.edit_mail_no);
    }
    @OnClick({R.id.activity_titlebar_left, R.id.act_login_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.act_login_submit:
                //填写邮寄地址
                initData(getIntent().getStringExtra("order_id"));

                break;

        }
    }
    private void initData(String order_id ){
        Map<String, Object> params = new HashMap<>();
        params.put(Constants.USER_ID, MyPerference.getInstance().getUserId());
        params.put(Constants.ORDER_ID, order_id);
        params.put("return_carriage_number", editOrderAddress.getText().toString());
        HttpManager.getInstance().perform_post(API.RETURN_NUMBER, params, new MyStringResponse(this) {
            @Override
            public void onCompleted(Object tag){
                super.onCompleted(tag);
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                Toast.makeText(EditOrderNoActivity.this, codeModel.getMessage(), Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}
